#include "ServerSocket.h"

ServerSocket::ServerSocket()
{

}

bool ServerSocket::Open(int port)
{
	SDLNet_ResolveHost(&m_ip, INADDR_ANY, port);
	//return m_ip.host == INADDR_NONE;

	if (m_ip.host == INADDR_NONE)
	{
		return false;
	}
	else
	{
		m_socket = SDLNet_TCP_Open(&m_ip);
		if (!m_socket)
		{
			return false;
		}
	}

	return true;
}

Socket* ServerSocket::Accept()
{
	//Socket* s;
	//TCPsocket socket;
	//while(!socket)
	//{
	//	socket = SDLNet_TCP_Accept(socket);
	//	SDL_Delay(1000);
	//}
	//s->SetSocket(socket);

	TCPsocket socket = nullptr;
	while(!socket)
	{
		socket = SDLNet_TCP_Accept(socket);
		SDL_Delay(1000);
	}
	
	return new Socket(socket);
}